package com.byxy.service.impl;

import java.util.List;

import javax.annotation.Resource;
import javax.transaction.Transactional;

import org.springframework.stereotype.Service;

import com.byxy.dao.AgencyDao;
import com.byxy.entity.Agency;
import com.byxy.entity.Guide;
import com.byxy.entity.Order;
import com.byxy.service.AgencyService;

@Service
@Transactional
public class AgencyServiceImpl implements AgencyService {
	@Resource
	private AgencyDao agencyDao;

	@Override
	public List<Agency> listAll() {
		return agencyDao.find();
	}

	@Override
	public void delete(int id) {
		Agency s = agencyDao.get(id);
		for (Guide guide : s.getGuides()) {
			guide.setAgency(null);
		}
		for (Order order : s.getOrders()) {
			order.setAgency(null);
		}
		agencyDao.delete(s);

	}

	@Override
	public void save(Agency agency) {
		agencyDao.add(agency);
	}

	@Override
	public Agency getById(int id) {
		return agencyDao.get(id);
	}

	@Override
	public void update(Agency agency) {
		agencyDao.update(agency);

	}

	@Override
	public List<Agency> listPage(int page, int rows) {
		int pos = (page - 1) * rows;
		return agencyDao.find(pos, rows);
	}

	@Override
	public long count() {
		return agencyDao.count();
	}

}
